import { fileURLToPath, URL } from 'node:url'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import path from 'path'

const devProxyUrl = 'http://localhost:8082';  //本地
// const devProxyUrl = 'http://43.139.72.200:8082';  //本地线上
export default defineConfig({
  plugins: [           
    vue(),
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver()],
    }),
    createSvgIconsPlugin({
      // 指定需要缓存的图标文件夹
      iconDirs: [path.resolve(process.cwd(), 'src/assets/svg')],
      // 指定symbolId格式
      symbolId: 'icon-[dir]-[name]',
    })
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  },
  server: {
    port: 8083,
    host: true,
    // 设置 https 代理
    proxy: {
      '/proxy_api': {
        target: devProxyUrl,
        changeOrigin: true,
        rewrite: (path) => {
          console.log(`请求${path}，代理到${devProxyUrl}${path}`);
          return path.replace(/^\/proxy_api/, '');
        }
      }
    }
  }
})
